package cn.fantasy.jdbc;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class JavaJdbc {
    private static String username;
    private static String password;
    private static String driver;
    private static String jdbcurl;
    static {
        try {
            FileInputStream fis = new FileInputStream("src/main/resources/db.properties");
            Properties pro = new Properties();
            pro.load(fis);
            fis.close();
            driver = pro.getProperty("jdbc.driver");
            jdbcurl = pro.getProperty("jdbc.url");
            username = pro.getProperty("jdbc.user");
            password = pro.getProperty("jdbc.password");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(jdbcurl, username, password);
            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from admin");
            while (rs.next()) {
                System.out.println(rs.getInt("id") + rs.getString("username"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
